﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Entity;
using IBusinessLogicLayer;

namespace MVC.Controllers
{
    public class StoreManagerController : Controller
    {
        private IBaseBll<Album> albumBll;
        private IBaseBll<Artist> artistBll;
        private IBaseBll<Genre> genreBll; 

        public StoreManagerController(IBaseBll<Album> albumBll, IBaseBll<Artist> artistBll, IBaseBll<Genre> genreBll)
        {
            this.albumBll = albumBll;
            this.artistBll = artistBll;
            this.genreBll = genreBll;
        } 
        // GET: StoreManager
        public ActionResult Index()
        {
            var albums = albumBll.GetQueryable().Include(c => c.Artist).Include(c=>c.Genre).ToList();
            return View(albums);
        }

        // GET: StoreManager/Details/5
        public ActionResult Details(int id)
        {
            var album = albumBll.FindSingle(a => a.Id == id);

            return View(album);
        }

        // GET: StoreManager/Create
        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(genreBll.GetQueryable().ToList(),"Id","Name");
            ViewBag.ArtistId=new SelectList(artistBll.GetQueryable().ToList(),"Id","Name");
            return View();
        }

        // POST: StoreManager/Create
        [HttpPost]
        public ActionResult Create(Album album)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    albumBll.Insert(album);
                    return RedirectToAction("Index");
                }  
            }
            catch
            {
                //log 
            }
            ViewBag.GenreId = new SelectList(genreBll.GetQueryable().ToList(), "Id", "Name");
            ViewBag.ArtistId = new SelectList(artistBll.GetQueryable().ToList(), "Id", "Name");
            return View(album);
        }

        // GET: StoreManager/Edit/5
        public ActionResult Edit(int id)
        {
            var album = albumBll.FindSingle(a => a.Id == id);
            ViewBag.GenreId = new SelectList(genreBll.GetQueryable().ToList(), "Id", "Name",id);
            ViewBag.ArtistId = new SelectList(artistBll.GetQueryable().ToList(), "Id", "Name",id);
            return View(album);
        }

        // POST: StoreManager/Edit/5
        [HttpPost]
        public ActionResult Edit(Album album)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    albumBll.Update(a => a.Id == album.Id, entity => new Album()
                    {
                        ArtistId = album.ArtistId,
                        Title = album.Title,
                        GenreId = album.GenreId,
                        Price = album.Price,
                        ArtUrl = album.ArtUrl
                    });
                    return RedirectToAction("Index");
                }  
            }
            catch
            {
                //log
            }
            ViewBag.GenreId = new SelectList(genreBll.GetQueryable().ToList(), "Id", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(artistBll.GetQueryable().ToList(), "Id", "Name", album.ArtistId);
            return View();
        }

        // GET: StoreManager/Delete/5
        public ActionResult Delete(int id)
        {
            var album = albumBll.FindSingle(a => a.Id == id);
            return View(album);
        }

        // POST: StoreManager/Delete/5
        [HttpPost,ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                // TODO: Add delete logic here
                albumBll.Delete(c => c.Id == id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
